package leetcode;

import java.util.*;

public class LC9 {
    public static void main(String[] args) {
        LC9 lc9 = new LC9();
        System.out.println(lc9.findAnagrams("cbaebabacd","abc"));
    }
    public List<Integer> findAnagrams(String s, String p) {
        int[] ss = new int[26];
        int[] pp = new int[26];
        List<Integer> ans = new ArrayList<>();
        for (int i = 0; i < p.length(); i++) {
            ss[s.charAt(i) - 'a'] ++;
            pp[p.charAt(i) - 'a'] ++;
        }
        if (Arrays.equals(ss,pp)) ans.add(0);
        for (int i = 0; i < s.length() - p.length(); i++) {
            ss[s.charAt(i) - 'a']--;
            ss[s.charAt(i + p.length()) - 'a']++;
            if (Arrays.equals(ss,pp)) ans.add(i+1);
        }
        return ans;
    }
}
